package gcontent.action;

import java.util.ArrayList;
import java.util.Calendar;

import gcontent.Results.Results;
import gcontent.action.SearchType;
import gcontent.db.BaseDB;
import gcontent.db.Search.Searches;
import gcontent.db.Search.Searches;
import gcontent.db.statistics.Statistics;
import com.opensymphony.xwork2.*;

public class Search extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private final int recordsPerPage = 15;
	private int totalRecordCount = 0;
	private int currentPage = 0;
	private int totalPages = 0;	
	private String search = ""; 	
	private String time_to_run = "0";
	private int searchType = SearchType.TYPE_CONTENT;	
	private Results results = new Results();
	
	public String getNumRowsString() 
	{
		return "Found " + String.valueOf(totalRecordCount) + " records " + 
		 	"for string '" + this.search + "' in " + time_to_run + " millis";
	}	
	
	public void setSearch(String search_str)
	{
		this.search = search_str;		
	}
	
	public int getRecordsPerPage() {
		return recordsPerPage;
	}

	public int getTotalRecordCount() {
		return totalRecordCount;
	}

	public int getCurrentPage() {
		return currentPage;
	}
	
	public void setCurrentPage(int current_page) {
		currentPage = current_page;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public String getSearch()
	{
		return this.search;
	}
	
	/**
	 * Register the search that user made
	 */
	private void addSearch() {
		if (!this.search.equals("")) {
			try {
				Statistics st = new Statistics();
				st.AddSearch(search, searchType);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}	
	
	private Results makeSearch() {
		if (!this.search.equals("")) {
			try {
				Searches searches = new Searches(true);
				return searches.makeSearch(search, searchType);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public int getSearchType() {
		return searchType;
	}

	public void setSearchType(int searchType) {
		this.searchType = searchType;
	}

	public ArrayList<SearchType> getTypes()
	{
		ArrayList<SearchType> list = new ArrayList<SearchType>();
		list.add(new SearchType(SearchType.TYPE_CONTENT, "Content"));
		list.add(new SearchType(SearchType.TYPE_IMAGES, "Images"));
		list.add(new SearchType(SearchType.TYPE_VIDEOS, "Videos"));
		list.add(new SearchType(SearchType.TYPE_TORRENTS, "Torrents"));
		list.add(new SearchType(SearchType.TYPE_ELINKS, "eLinks"));
		return list;
	}

	public Results getResults() {
		return results;
	}
	
	public String execute() {
		this.addSearch();
		double time_to_execute = 0;
		
		// Get the results
		Calendar cal = Calendar.getInstance();
		time_to_execute = cal.getTimeInMillis();
		
		results = this.makeSearch();
		
		if (results != null) {			
			totalRecordCount = results.getCount();
			totalPages = (totalRecordCount / recordsPerPage) + 1;			
			results.parseResults(currentPage * recordsPerPage, (currentPage * recordsPerPage + recordsPerPage), results);
		}
		
		cal = Calendar.getInstance();
		time_to_execute = cal.getTimeInMillis() - time_to_execute;
		time_to_run = String.valueOf(time_to_execute);
		
		return SUCCESS;
	}
}
